package com.yjy.mdc.wrapper;

import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * CompletableFuture MDC包装类
 */
public class CompletableFutureMdcWrapper {
    public static CompletableFuture<Void> runAsync(Runnable runnable) {
        Map<String, String> contextMap = MDC.getCopyOfContextMap();
        return CompletableFuture.runAsync(() -> {
            try {
                if (contextMap != null) {
                    MDC.setContextMap(contextMap);
                }
                runnable.run();
            } finally {
                MDC.clear();
            }
        });
    }
}
